home *** CD-ROM | disk | FTP | other *** search
- /*
- my.js
-
- Copyright © 2005, 2006, 2007, 2008 Against Intuition, Inc. <info@mywot.com>
- */
-
- var wot_my_session =
- {
- init: function()
- {
- try {
- if (this.uri && this.cs) {
- return;
- }
-
- var ios = Components.classes["@mozilla.org/network/io-service;1"].
- getService(Components.interfaces.nsIIOService);
-
- this.uri = ios.newURI("http://" + WOT_MY_COOKIE_DOMAIN + "/",
- "", null);
-
- this.domain = "; domain=" + WOT_MY_COOKIE_DOMAIN;
-
- this.cs = Components.classes["@mozilla.org/cookieService;1"].
- getService(Components.interfaces.nsICookieService);
-
- window.addEventListener("unload", function(e) {
- wot_my_session.unload();
- }, false);
- } catch (e) {
- dump("wot_my_session.init: failed with " + e + "\n");
- }
- },
-
- unload: function()
- {
- this.ios = null;
- this.uri = null;
- this.cs = null;
- },
-
- clear: function()
- {
- try {
- var mgr = Components.classes["@mozilla.org/cookiemanager;1"].
- getService(Components.interfaces.nsICookieManager);
- if (mgr) {
- mgr.remove(WOT_MY_COOKIE_DOMAIN, "id", "/", false);
- mgr.remove(WOT_MY_COOKIE_DOMAIN, "nonce", "/", false);
- mgr.remove(WOT_MY_COOKIE_DOMAIN, "auth", "/", false);
- mgr.remove(WOT_MY_COOKIE_DOMAIN, "authid", "/", false);
- mgr.remove(WOT_MY_COOKIE_DOMAIN, "reload", "/", false);
- }
- } catch (e) {
- dump("wot_my_session.clear: failed with " + e + "\n");
- }
- },
-
- update: function(force)
- {
- try {
- if (!wot_api_register.ready || !this.uri || !this.cs) {
- return;
- }
-
- this.cs.setCookieString(this.uri, null, "accessible=" +
- wot_prefs.accessible + this.domain, null);
-
- if (!wot_prefs.my_cookies) {
- if (force) {
- this.clear();
- }
- return;
- }
-
- /* If it has been WOT_MY_SESSION_LENGTH seconds since the
- session was last updated, force an update */
- if ((Date.now() - Number(wot_prefs.cookie_updated)) >
- WOT_MY_SESSION_LENGTH) {
- force = true;
- }
-
- /* If we have an authid cookie set by the server (for any id),
- don't update unless forced */
- var authid = new RegExp("authid=\\w{" +
- WOT_LENGTH_WITNESS_ID + "}");
- var current = this.cs.getCookieString(this.uri, null);
-
- if (!force && current && current.match(authid)) {
- return;
- }
-
- /* Update authentication cookies */
- var id = "id=" + wot_prefs.witness_id;
- this.cs.setCookieString(this.uri, null, id + this.domain, null);
-
- var nonce = "nonce=" + wot_crypto.nonce();
- this.cs.setCookieString(this.uri, null, nonce + this.domain, null);
-
- var auth = "auth=" +
- wot_crypto.authenticate(id + "&" + nonce);
- this.cs.setCookieString(this.uri, null, auth + this.domain, null);
-
- /* Update time */
- wot_prefs.setChar("cookie_updated", Date.now().toString());
- } catch (e) {
- dump("wot_my_session.update: failed with " + e + "\n");
- }
- },
-
- reload: function()
- {
- try {
- if (!wot_api_register.ready || !wot_prefs.my_cookies) {
- return;
- }
-
- var current = this.cs.getCookieString(this.uri, null);
-
- if (!current) {
- return;
- }
-
- var reload = new RegExp("reload=(\\w{" +
- WOT_LENGTH_WITNESS_ID + "})");
-
- var match = current.match(reload);
-
- /* Reload if we have a reload cookie, but with a different id than
- ours */
- if (match && match[1] &&
- match[1] != wot_prefs.witness_id) {
- wot_api_reload.send(match[1]);
- }
- } catch (e) {
- dump("wot_my_session.reload: failed with " + e + "\n");
- }
- }
- };
-
- wot_my_session.init();
-